<template>
    <div class="FMenu" :style="{ width: userStore.asideWidth }">
        <el-menu :default-active="defaultActive" unique-opened :collapse="isCollapse" default-active="2" class="FMenuBody"
            @select="handleSelect" :collapse-transition="false">
            <template v-for="(item, index) in asideMenu" :key="index">
                <el-sub-menu v-if="item.child && item.child.length > 0" :index="item.name">
                    <template #title>
                        <el-icon>
                            <component :is="item.icon"></component>
                        </el-icon>
                        <span>{{ item.name }}</span>
                    </template>
                    <el-menu-item v-for="(item2, index2) in item.child" :key="index2" :index="item2.frontpath">
                        <el-icon>
                            <component :is="item2.icon"></component>
                        </el-icon>
                        <span>{{ item2.name }}</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-menu-item v-else :index="item.frontpath">
                    <el-icon>
                        <component :is="item.icon"></component>
                    </el-icon>
                    <span>{{ item.name }}</span>
                </el-menu-item>
            </template>
        </el-menu>
    </div>
</template>
<script setup>
import { ref } from 'vue'
import { computed } from 'vue'
import { useUserStore } from '@/stores/user'
import { useRouter, useRoute, onBeforeRouteUpdate } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

// 默认选中菜单
const defaultActive = ref(route.path)

// 监听路由变化
onBeforeRouteUpdate((to, from) => {
    defaultActive.value = to.path
})

// 菜单数据
const asideMenu = computed(() => userStore.menus)

// 菜单路由跳转
const handleSelect = (e) => {
    router.push(e)
}

// 是否折叠
const isCollapse = computed(() => !(userStore.asideWidth === "250px"))
</script>
<style scoped lang="less">
.FMenu {
    position: fixed;
    bottom: 0;
    left: 0;
    top: 64px;
    overflow: auto;
    background-color: #fff;
    box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
    transition: all 0.3s;
    overflow-y: auto;
    overflow-x: hidden;
    overflow-x: hidden;

    &::-webkit-scrollbar {
        width: 0px;
    }

    .FMenuBody {
        border: 0
    }
}
</style>